package com.lufei4.chestnut.view;

import android.app.Application;
import android.content.Context;
import android.util.Log;

import androidx.annotation.Keep;

import com.taobao.sophix.PatchStatus;
import com.taobao.sophix.SophixApplication;
import com.taobao.sophix.SophixEntry;
import com.taobao.sophix.SophixManager;
import com.taobao.sophix.listener.PatchLoadStatusListener;
/**
 * Sophix入口类，专门用于初始化Sophix，不应包含任何业务逻辑。
 * 此类必须继承自SophixApplication，onCreate方法不需要实现。
 * 此类不应与项目中的其他类有任何互相调用的逻辑，必须完全做到隔离。
 * AndroidManifest中设置application为此类，而SophixEntry中设为原先Application类。
 * 注意原先Application里不需要再重复初始化Sophix，并且需要避免混淆原先Application类。
 * 如有其它自定义改造，请咨询官方后妥善处理。
 *
 */
public class SophixStubApplication extends SophixApplication {
    private final String TAG = "SophixStubApplication";
    // 此处SophixEntry应指定真正的Application，并且保证RealApplicationStub类名不被混淆。
    @Keep
    @SophixEntry(MyApp.class)
    static class RealApplicationStub {}
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        //         如果需要使用MultiDex，需要在此处调用。
        //         MultiDex.install(this);
        initSophix();
    }
    private void initSophix() {
        String appVersion = "1.0.0";//版本号
        try {
            appVersion = this.getPackageManager()
                    .getPackageInfo(this.getPackageName(), 0)
                    .versionName;
        } catch (Exception e) {
        }
        final SophixManager instance = SophixManager.getInstance();
        instance.setContext(this)
                .setAppVersion(appVersion)
                .setSecretMetaData("31332730", "78c264dbd9c17daec78092ba09f238a9", "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCoTTzRhKk/RUEUYyPaAGguDTNzh8HUZ6G+VtmglQXEZW9ln5pbjsXIegNoq97rCya35CFc2w/q9LN9kVED5rmndVL1jhJg3iBssXlDyWMuRu0OAWp2PYa4NrDxECXHFKtbfeNbupo0zpkJtM54phrdhrjO6OBKwfA44d3scqouH1KTEmpRrYAi43nj7W8CgQm2ivo6+iSMi9OVbgudkvYNU+ZE5vtuF+YyJ4fthBmB+B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IHL9NER+k/DlAI5MlmA0u4OLGuoG7NIxjfA2NOubm+rRKG78dXDPcr+dTRDyaOxS3qyptdIPWxAoGAWBcAGs7e9oH+nD1zcll2KSjvzkpH3FQlzN97ayJf+UsqW5Xoo/ZpS4Pk35E65N+kOT1DtEDD5w0W42UONgMAw60p6pGzy4FG/lKF1wKg4cI5ANf0TopEXNRfX6QzAw7aETMPunWungOEwbZS8mi3BDfBP88D7ErxQKYGf8sPAWA=")
                .setEnableDebug(true)
                .setEnableFullLog()
                .setPatchLoadStatusStub(new PatchLoadStatusListener() {
                    @Override
                    public void onLoad(final int mode, final int code, final String info, final int handlePatchVersion) {
                        if (code == PatchStatus.CODE_LOAD_SUCCESS) {
                            Log.i(TAG, "sophix load patch success!");
                        } else if (code == PatchStatus.CODE_LOAD_RELAUNCH) {
                            // 如果需要在后台重启，建议此处用SharePreference保存状态。
                            Log.i(TAG, "sophix preload patch success. restart app to make effect.");
                        }
                    }
                }).initialize();
    }

    @Override
    public void onCreate() {
        super.onCreate();
        SophixManager.getInstance().queryAndLoadNewPatch();
    }
}